package com.example.lenovo.musicplayer;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;

public class MainActivity extends AppCompatActivity {
    private SeekBar sb_progress;
    private MusicPlayerService.MyBinder musicControl;
    private MyConnection conn;
    private ImageButton ib_pre;
    private ImageButton ib_play;
    private ImageButton ib_next;
    private String path = "http://192.168.49.36:8080/01.mp3";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sb_progress = (SeekBar) findViewById(R.id.sb_progress);
        ib_pre = (ImageButton) findViewById(R.id.ib_pre);
        ib_play = (ImageButton) findViewById(R.id.ib_play);
        ib_next = (ImageButton) findViewById(R.id.ib_next);

        Intent intent = new Intent(getApplicationContext(),MusicPlayerService.class);
        conn = new MyConnection();
        bindService(intent,conn,BIND_AUTO_CREATE);
    }
    private class MyConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //  在这个方法中一定会获取到musicControl，
            // 因为所有控制音乐的方法都需要使用MyBiner来调用，所以初始化界面方法都写在这里
            musicControl = (MusicPlayerService.MyBinder) service;
            updatePlayIcon();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
    public void play(View v ) {
        new Thread() {
            @Override
            public void run() {

            }
        }.start();
        musicControl.playPause();
        updatePlayIcon();
    }
    public void updatePlayIcon() {
        if(musicControl.isPlaying()) {
            ib_play.setImageResource(R.drawable.pause);
        } else {
            ib_play.setImageResource(R.drawable.play);
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindService(conn);
    }
}
